package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.adapter.LvRepairDetailAdapter;
import com.mxnet.chongjin.supoman.bean.RepairRecord;
import com.mxnet.chongjin.supoman.utils.DpAndPxUtils;

import java.util.ArrayList;
import java.util.List;

public class RepairDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout, mLayout;
    private LvRepairDetailAdapter mAdapter;
    private ListView mListView;
    private List<RepairRecord.MaintenanceRecordsBean> mList;
    private List<Bitmap> mViewList;
    private RepairRecord mRepairRecord;
    private TextView mTvOrderNum, mTvXulieNum, mTvMachineInfo, mTvPartnerName, mTvPartnerTel, mTvPartnerAddr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_detail);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = RepairDetailActivity.this;
        Bundle bundle = getIntent().getExtras();
        mRepairRecord = bundle.getParcelable("record");

        initView();
        initData();
    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_repair_detail);
        mLinearLayout.setOnClickListener(this);

        mListView = findViewById(R.id.lv_repair_detail);

        mTvOrderNum = findViewById(R.id.tv_repair_detail_order_num);
        mTvOrderNum.setText(getResources().getString(R.string.repair_num) + "  " + mRepairRecord.getDeclarationNo());

        mTvXulieNum = findViewById(R.id.tv_repair_detail_xulie_num);
        mTvXulieNum.setText(getResources().getString(R.string.serial_number) + "  " + mRepairRecord.getProjectNo());

        mTvMachineInfo = findViewById(R.id.tv_repair_detail_machine_info);
        mTvMachineInfo.setText(mRepairRecord.getDeclarationMemo());

        mTvPartnerName = findViewById(R.id.tv_repair_detail_partner_name);
        mTvPartnerName.setText(getResources().getString(R.string.partner_name) + "  " + mRepairRecord.getCustomerName());

        mTvPartnerTel = findViewById(R.id.tv_repair_detail_partner_tel);
        mTvPartnerTel.setText(getResources().getString(R.string.contact) + "  " + mRepairRecord.getCustomerMobile());

        mTvPartnerAddr = findViewById(R.id.tv_repair_detail_partner_addr);
        mTvPartnerAddr.setText(getResources().getString(R.string.contact_addr) + "  " + mRepairRecord.getCountry() + mRepairRecord.getCity() + mRepairRecord.getDistirct() + mRepairRecord.getDetailAddress());

        mLayout = findViewById(R.id.ll_repair_detail_faults);
    }

    public void initData() {
        mViewList = new ArrayList<>();

//        mRepairRecord.getImgResource().getUrl();
        mRepairRecord.getResourceIdList();

        mList = new ArrayList<>();
        mList = mRepairRecord.getMaintenanceRecords();

        mAdapter = new LvRepairDetailAdapter(mContext, mList);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_repair_detail:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }

    private void addImageView(LinearLayout linearLayout) {
        linearLayout.removeAllViews();
        LinearLayout layout = null;
        for (int i = 0, j = mViewList.size() - 1; i < mViewList.size() && j >= 0; i++, j--) {
            ImageView iv = new ImageView(mContext);
            iv.setImageBitmap(mViewList.get(j));
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                    DpAndPxUtils.dip2px(mContext, 75f),
                    DpAndPxUtils.dip2px(mContext, 75f));
            params1.setMargins(0, DpAndPxUtils.dip2px(mContext, 15f), DpAndPxUtils.dip2px(mContext, 15f), 0);
            iv.setLayoutParams(params1);
            if (i % 4 == 0) {
                layout = new LinearLayout(mContext);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.addView(layout);
            }
            layout.addView(iv);
        }
    }
}
